﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Easily
{
    public partial class frmLCD : Form
    {
        private GeneralIcon MyIcon = null;  //保存传入的控件对象 
        /// <summary>
        ///构造函数
        ///主要功能：加载保存的控件设置；生成初始代码；
        /// </summary>
        public frmLCD(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;

            //添加头文件
            this.MyIcon.code.HeadFile = "#include \"lcd.h\"";
            //添加可供选择的系统变量到cBox_var中
            if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
            {
                //全局变量
                for (int i = 0; i < Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys.Count; i++)
                {
                    cBox_var.Items.Add(Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys[i]);
                }
                //传感器局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_Sensor_VariableLocal.Values)
                {
                    cBox_var.Items.Add(item);
                }
                //局部变量
                foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableLocal)
                {
                    cBox_var.Items.Add(item);
                }
            }

            if (MyIcon.code.Save != "")
            {
                //保存的参数共有5项，以英文分号';'分开
                string[] tmp = MyIcon.code.Save.Split(';');
                txt_string.Text = tmp[0];
                txt_posX.Text = tmp[1];
                txt_posY.Text = tmp[2];
                
            }
            //默认显示
            else
            {
                txt_string.Text = "Easily!";
                txt_posX.Text = "0";
                txt_posY.Text = "0";            
            }
            btn_confirm_Click(new object(), new EventArgs());//触发确认

        }

        private void frmLCD_Load(object sender, EventArgs e)
        {
            cBox_fontColor.Items.Clear();
            cBox_gbColor.Items.Clear();
            cBox_fontColor.Items.Add("BLUE");
            cBox_fontColor.Items.Add("GRAY");
            cBox_fontColor.Items.Add("RED");
            cBox_gbColor.Items.Add("BLUE");
            cBox_gbColor.Items.Add("GRAY");
            cBox_gbColor.Items.Add("RED");
            //添加ch
            if (MyIcon.Parent.ModuleName == "接收到串口数据")
            {
                cBox_var.Items.Add("&ch");
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            MyIcon.code.Clear();//清空以前代码
            MyIcon.code.Initialization = "LCD_Init();    //LCD初始化";
            string fontColor = "";//字体颜色
            string gbColor = "";//字体背景颜色
            if (cBox_fontColor.SelectedItem == null || cBox_gbColor.SelectedItem == null)
            {
                fontColor = "BLUE";
                gbColor = "GRAY";
            }
            else
            {
                fontColor = cBox_fontColor.SelectedItem.ToString();
                gbColor = cBox_gbColor.SelectedItem.ToString();
            }
            //如果要显示的是变量
            if (cBox_var.SelectedIndex != -1)
            {
                //MyIcon.code.GlobalVariable = "uint8_t g10LCDTemp[30];\r\n";
                string temp = cBox_var.SelectedItem.ToString();
                //MyIcon.code.InsertCode = "const uint8_t * str=&"+temp+";";
                MyIcon.code.InsertCode += "\n\tLCD_ShowString(" + txt_posX.Text.ToString() + "," + txt_posY.Text.ToString() + "," +
                 fontColor + "," + gbColor + "," + temp + ");";
            }
            else
            {
                MyIcon.code.InsertCode = "\n\tLCD_ShowString(" + txt_posX.Text.ToString() + "," + txt_posY.Text.ToString() + "," +
                     fontColor + "," + gbColor + ",\"" + txt_string.Text.ToString() + "\");";
            }
            MyIcon.code.Save = txt_string.Text.ToString() + ";" + txt_posX.Text.ToString() + ";" + txt_posY.Text.ToString() + ";"
                                + fontColor + ";" + gbColor;
            MyIcon.code.Comment = "LCD显示" + "\"" + txt_string.Text.ToString() + "\"";

            //外部会对此进行判断，因此是必要的
            this.DialogResult = DialogResult.OK;

        }

        /// <summary>
        /// 在文本框被聚焦时，按下按键触发
        /// 主要功能：设置不能输入英文分号';'
        /// </summary>
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ';')
                e.Handled = true;  //表明按键事件已处理
        }

        /// <summary>
        /// 单击“取消”按钮
        /// 主要功能：设置返回结果为取消
        /// </summary>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 点击“注释说明”按钮
        /// 功能概要：为控件添加自定义注释说明
        /// </summary>
        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                    this.MyIcon.code.Comment = NE.NoteEdit;
            }
        }

    }
}
